package com.blackjack.cards
{
	import com.blackjack.Game;
	
	import flash.display.Sprite;

	public class Stack
	{
		private static var _instance:Stack;
		private var _stack:Array;
		
		public function Stack()
		{
			initStack();
			shuffleStack();
		}
		
		public static function get instance():Stack{
			if(!_instance) _instance = new Stack();
			return _instance;
		}
		
		public function resetStack():void{
			_stack = null;
			initStack();
		}
		
		public function get stack():Array{return _stack;}
		
		public function takeCard():Card{
			shuffleStack();
			trace("Number of Cards in Stack:"+(_stack.length-1));
			if(_stack) return _stack.pop();
			return null;
		}
		
		public function shuffleStack():void{
			var newArray:Array = new Array();
			while(_stack.length > 0){
				var randomIndex:uint = Math.floor(Math.random()*_stack.length);
				newArray.push(_stack[randomIndex]);
				_stack.splice(randomIndex,1);
			}
			_stack = newArray;
		}
		
		private function initStack():void{
			_stack = new Array();
			for(var i:int = 0;i < Game.CARDPACKS_IN_STACK ;i++){
				_stack.push(new Card("Club","2"));
				_stack.push(new Card("Diamond","2"));
				_stack.push(new Card("Heart","2"));
				_stack.push(new Card("Spade","2"));
				_stack.push(new Card("Club","3"));
				_stack.push(new Card("Diamond","3"));
				_stack.push(new Card("Heart","3"));
				_stack.push(new Card("Spade","3"));
				_stack.push(new Card("Club","4"));
				_stack.push(new Card("Diamond","4"));
				_stack.push(new Card("Heart","4"));
				_stack.push(new Card("Spade","4"));
				_stack.push(new Card("Club","5"));
				_stack.push(new Card("Diamond","5"));
				_stack.push(new Card("Heart","5"));
				_stack.push(new Card("Spade","5"));
				_stack.push(new Card("Club","6"));
				_stack.push(new Card("Diamond","6"));
				_stack.push(new Card("Heart","6"));
				_stack.push(new Card("Spade","6"));
				_stack.push(new Card("Club","7"));
				_stack.push(new Card("Diamond","7"));
				_stack.push(new Card("Heart","7"));
				_stack.push(new Card("Spade","7"));
				_stack.push(new Card("Club","8"));
				_stack.push(new Card("Diamond","8"));
				_stack.push(new Card("Heart","8"));
				_stack.push(new Card("Spade","8"));
				_stack.push(new Card("Club","9"));
				_stack.push(new Card("Diamond","9"));
				_stack.push(new Card("Heart","9"));
				_stack.push(new Card("Spade","9"));
				_stack.push(new Card("Club","10"));
				_stack.push(new Card("Diamond","10"));
				_stack.push(new Card("Heart","10"));
				_stack.push(new Card("Spade","10"));
				_stack.push(new Card("Club","Jack"));
				_stack.push(new Card("Diamond","Jack"));
				_stack.push(new Card("Heart","Jack"));
				_stack.push(new Card("Spade","Jack"));
				_stack.push(new Card("Club","Queen"));
				_stack.push(new Card("Diamond","Queen"));
				_stack.push(new Card("Heart","Queen"));
				_stack.push(new Card("Spade","Queen"));
				_stack.push(new Card("Club","King"));
				_stack.push(new Card("Diamond","King"));
				_stack.push(new Card("Heart","King"));
				_stack.push(new Card("Spade","King"));
				_stack.push(new Card("Club","Ace"));
				_stack.push(new Card("Diamond","Ace"));
				_stack.push(new Card("Heart","Ace"));
				_stack.push(new Card("Spade","Ace"));
			}
		}
	}
}